#include <boost/algorithm/string.hpp>
#include <boost/functional/hash.hpp>

typedef std::pair<size_t,Entry*> HASH_ENTRY_REF_PAIR;
typedef unordered_map<size_t, Entry*> HASH_ENTRY_REF_MAP;

struct Bucket2 {
	int key;
	Bucket2 * prev,* next;
	HASH_ENTRY_REF_MAP elements;

	Bucket2(int _key, Bucket2 *_prev, Bucket2 *_next ) {
		key = _key;
		prev = _prev;
		next = _next;
	}

	~Bucket2(){
		HASH_ENTRY_REF_MAP::iterator it;
		for (it = elements.begin(); it != elements.end(); it++) {
			delete it->second;
		}
	}

	void add(Entry *e) {
//		printf("add: has entry: %d\n", (elements.find(e->hash) != elements.end()));
		elements.insert(HASH_ENTRY_REF_PAIR(e->hash, e));
		//prinf("My #el : %d\n", elements.size());
	}

	void remove(Entry *e) {
		HASH_ENTRY_REF_MAP::iterator it = elements.find(e->hash);
		//prinf("remove: has entry: %d\n", (it != this->elements.end()));
		elements.erase(it);
	}

	bool has_elements() {
		return (elements.size() > 0);
	}

	bool operator<(const Bucket2& rhs) {
			return key < rhs.key;
	}

	bool operator==(const Bucket2& rhs) {
		return key == rhs.key;
	}

	size_t hashvalue() const {
	    boost::hash<int> hasher;
	    return hasher(key);

	}

	string to_string() {
		stringstream ss;
		HASH_ENTRY_REF_MAP::iterator it = elements.begin();
		int _prev = prev != NULL ? prev->key : -1;
		int _next = next != NULL ? next->key : -1;
		ss << "bucket " << key << ": " << _prev << ":" << _next <<  ":";
		for (; it != elements.end(); it++) {
			ss << it->second->to_string() << " ";
		}
		return ss.str();
	}
};
